package cn.microven.mindmap.oprations;

import cn.microven.mindmap.components.part.MapPart;
import cn.microven.mindmap.components.line.MapLine;
import cn.microven.mindmap.components.part.Part;
import cn.microven.mindmap.struture.MindMap;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.util.Pair;

import java.util.List;

public class RemoveLeafOperation implements Operation{
    private boolean isUserOperation = true;
    private List<Pair<MapLine, Part>> list;
    private VBox vBox;

    private Part root;

    private Pair<MapLine, Part> leaf;

    private Pane linesBox;

    /**
     * 构造方法
     *
     * @param vBox 子节点加入的 vBox
     * @param list 子节点列表
     * @param leaf 线与子节点组成的键值对
     * @param linesBox 线加入的布局
     *
     */
    public RemoveLeafOperation(Part root, List<Pair<MapLine, Part>> list, VBox vBox, Pane linesBox, Pair<MapLine, Part> leaf) {
        this.root = root;
        this.vBox = vBox;
        this.leaf = leaf;
        this.list = list;
        this.linesBox = linesBox;
    }

    public RemoveLeafOperation(boolean isUserOperation,Part root, List<Pair<MapLine, Part>> list, VBox vBox, Pane linesBox, Pair<MapLine, Part> leaf) {
        this(root,list,vBox,linesBox,leaf);
        this.isUserOperation = isUserOperation;
    }
    @Override
    public void start() {
        start(false);
    }

    @Override
    public void start(boolean isRedo) {
        vBox.getChildren().remove((MapPart)leaf.getValue());
        linesBox.getChildren().remove(leaf.getKey());
        list.remove(leaf);
        root.getTreeItem().getChildren().remove(leaf.getValue().getTreeItem());

        //leaf.getValue().setParentPart(root);.removeIf(child -> child.getValue() == node);

        if (isUserOperation && !isRedo)
            MindMap.addOperation(this);
    }

    @Override
    public void undo() {
        AddLeafOperation alo=new AddLeafOperation(root,list,vBox,linesBox,leaf);
        alo.start(true);
    }
}
